package com.zzh.lib.views.transform;


import android.view.View;

import com.zzh.lib.views.utils.BannerUtils;

import androidx.annotation.NonNull;
import androidx.viewpager2.widget.ViewPager2;

/**
 * Created by ZZH on 2022/7/29.
 *
 * @Date: 2022/7/29
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description:
 */
public class OverlapPageTransformer implements ViewPager2.PageTransformer {

    private final int orientation;
    private final float minScale;
    private final float unSelectedItemRotation;
    private final float unSelectedItemAlpha;
    private final float itemGap;
    private final float scalingValue = 0.2f;

    public OverlapPageTransformer(int orientation, float minScale, float unSelectedItemRotation, float unSelectedItemAlpha, float itemGap) {
        this.orientation = orientation;
        this.minScale = minScale;
        this.unSelectedItemRotation = unSelectedItemRotation;
        this.unSelectedItemAlpha = unSelectedItemAlpha;
        this.itemGap = itemGap;
        if (minScale < 0 || minScale > 1.0) {
            throw new IllegalArgumentException("minScale 取值范围 1.0 to 0.0");
        }
        if (unSelectedItemAlpha < 0 || unSelectedItemAlpha > 1.0) {
            throw new IllegalArgumentException("unSelectedItemAlpha 取值范围 1.0 to 0.0");
        }

    }

    @Override
    public void transformPage(@NonNull View page, float position) {

        page.setElevation(-Math.abs(position));

        float delta = Math.max(1f - Math.abs(position * (1 - 0.5f)), 0.5f);

        if (unSelectedItemRotation != 0f) {
            float unSelectedItemRotationTmp;
            if (position > 0) {
                unSelectedItemRotationTmp = unSelectedItemRotation;
            } else {
                unSelectedItemRotationTmp = -unSelectedItemRotation;
            }
            float rotation = unSelectedItemRotationTmp * (1 - delta);
            page.setRotationY(rotation);
        }

        float scaleDelta = Math.abs(position * scalingValue);
        float scale = Math.max(1f - scaleDelta, minScale);

        page.setScaleX(scale);
        page.setScaleY(scale);
        float dp2px = BannerUtils.dp2pxFloat(itemGap / 2);

        if (ViewPager2.ORIENTATION_HORIZONTAL == orientation) {
            float tx = position * dp2px + ((position > 0) ? (-page.getWidth() * (1f - scale)) : (page.getWidth() * (1f - scale)));
            page.setTranslationX(tx);

        } else if (ViewPager2.ORIENTATION_VERTICAL == orientation) {
            float ty = position * dp2px + ((position > 0) ? (-page.getWidth() * (1f - scale)) : (page.getWidth() * (1f - scale)));
            page.setTranslationY(ty);
        } else {
            throw new IllegalArgumentException("orientation参数错误，取值ViewPager2.ORIENTATION_HORIZONTAL 或者 ViewPager2.ORIENTATION_VERTICAL ");
        }

        if (unSelectedItemAlpha != 1f) {
            if (position >= -1f && position <= 1f) {
                page.setAlpha(0.5f + ((1 - Math.abs(position)) * 0.5f));
            } else {
                page.setAlpha(0.5f / Math.abs(position * position));
            }
        }

    }
}
